﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Fyp.Filters;
using System.Web.Security;
using Microsoft.Web.WebPages.OAuth;
using WebMatrix.WebData;
using Fyp.Models;
using System.Globalization;

namespace Fyp.Controllers
{
    public class MeetingSchedulerController : Controller
    {
        //
        // GET: /MeetingScheduler/

        [AllowAnonymous]
        [Authorize]
        [InitializeSimpleMembership]
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult LogOff()
        {
            WebSecurity.Logout();

            return RedirectToAction("MeetingScheduler", "MeetingScheduler");
        }

        public ActionResult SearchMeeting()
        {
            return View("SearchMeeting");
        }

        [HttpPost]
        public ActionResult SearchMeeting(FormCollection c)
        {
            var word = c["search"];
            var db = new FYPEntities5();
            try
            {
                IQueryable<MeetingScheduler> q = from ms in db.MeetingSchedulers
                                                 where ms.MeetingTitle.Contains(word)
                                                 select ms;
                ViewData["data"] = q.ToList();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.InnerException);
            }
            return View("MeetingSchedulerList");
        }


        public ActionResult MeetingSchedulerList(int? id)
        {
            try
            {
                var db = new FYPEntities5();

                var q = from ms in db.MeetingSchedulers
                        select ms;

                ViewData["data"] = q.ToList();
            }
            catch (Exception e)
            {

                Console.WriteLine(e.InnerException);
            }
            return View("MeetingSchedulerList");
        }

        public ActionResult MeetingScheduler(int? id)
        {
            try
            {

                var db = new FYPEntities5();

                var query = from g in db.Groups
                            select g;
                ViewData["grp"] = query.ToList();

                var projectTitle = from title in db.Projects
                                   select title.Title;

                ViewData["projecttitle"] = projectTitle.ToList();

                if (id == null || id == 0)
                {
                    return View("MeetingScheduler");
                }
                else
                {
                    var q = from ms in db.MeetingSchedulers
                            where ms.MeetingId == id
                            select ms;

                    ViewData["data"] = q.ToList();

                    //Extracting project title
                    var getGid = (from m in db.MeetingSchedulers
                                  select m.GId).FirstOrDefault();


                    var getPid = (from g in db.Groups
                                  where getGid == g.PId
                                  select g.PId).FirstOrDefault();

                    var getTitle = (from p in db.Projects
                                    where getPid == p.PId
                                    select p.Title).FirstOrDefault();

                    ViewData["projtitle"] = getTitle;
                                   
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.InnerException);
            }

            return View("MeetingScheduler");
        }

        [HttpPost]
        public ActionResult MeetingScheduler(FormCollection c)
        {

            MeetingScheduler ms = new MeetingScheduler();
            Project pro = new Project();

            var userName = WebSecurity.CurrentUserName;

            var db = new FYPEntities5();

            try
            {
                if (WebSecurity.IsAuthenticated && Roles.IsUserInRole("Advisor"))
                {
                    ms.MeetingTitle = c["mtitle"];
                    ms.MeetingLocation = c["mlocation"];
                    ms.Guests = c["mguests"];
                    ms.MeetingDate = Convert.ToDateTime(c["mdate"]);
                    ms.StartTime = c["starttime"];
                    ms.EndTime = c["endtime"];
                    ms.DescriptionText = c["description"];
                    ms.CommentText = c["comment"];
                    ms.GId = Convert.ToInt32(c["grp"]);
                   
                    ms.MeetingType = c["mtype"];
                    ms.Reminder = c["reminder"];
                    ms.MeetingId = Convert.ToInt32(c["ID"]);
                  
                    var meetingtitle = ms.MeetingTitle;
                    var meetinglocation = ms.MeetingLocation;
                    var guests = ms.Guests;
                    var meetingdate = ms.MeetingDate.Value.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
                    var starttime = ms.StartTime;
                    var endtime = ms.EndTime;
                    var description = ms.DescriptionText;
                    var comment = ms.CommentText;
                    var gid = ms.GId;
                  
                    var meetingtype = ms.MeetingType;
                    var reminder = ms.Reminder;
                    var meetingid = ms.MeetingId;

                    ViewData["meetingtitle"] = meetingtitle;
                    ViewData["meetinglocation"] = meetinglocation;
                    ViewData["guests"] = guests;
                    ViewData["meetingdate"] = meetingdate;
                    ViewData["starttime"] = starttime;
                    ViewData["endtime"] = endtime;
                    ViewData["description"] = description;
                    ViewData["comment"] = comment;
                    ViewData["gid"] = gid;
                    ViewData["meetingtype"] = meetingtype;
                    ViewData["reminder"] = reminder;
                   
                    if (ms.MeetingId == null || ms.MeetingId == 0)
                    {
                        db.MeetingSchedulers.Add(ms);
                        db.SaveChanges();

                        var idKey = (from i in db.MeetingSchedulers
                                     orderby i.MeetingId descending
                                     select i.MeetingId).First();


                        int userId = WebSecurity.CurrentUserId;

                        Notification notification = new Notification();

                        notification.MeetingId = idKey;
                        notification.UserId = userId;
                        notification.GId = gid;
                        notification.NText = ms.MeetingTitle;
                        notification.CurrentDate = DateTime.Now;

                        db.Notifications.Add(notification);
                        db.SaveChanges();

                    }
                    else
                    {
                        var q = from mss in db.MeetingSchedulers
                                where mss.MeetingId == meetingid
                                select mss;

                        foreach (var val in q)
                        {

                            val.MeetingTitle = meetingtitle;
                            val.MeetingTitle = meetinglocation;
                            val.MeetingLocation = meetinglocation;
                            val.Guests = guests;
                            val.MeetingDate = DateTime.ParseExact(meetingdate, "dd/MM/yyyy", CultureInfo.InvariantCulture);
                            val.StartTime = starttime;
                            val.EndTime = endtime;
                            val.DescriptionText = description;
                            val.GId = gid;
                            val.MeetingType = meetingtype;
                            val.Reminder = reminder;
                            val.CommentText = comment;
                           
                        }

                        db.SaveChanges();
                    }
                }
                else
                {
                    ms.MeetingTitle = c["mtitle"];
                    ms.MeetingLocation = c["mlocation"];
                    ms.Guests = c["mguests"];


                    ms.MeetingDate = Convert.ToDateTime(c["mdate1"]);

                    ms.StartTime = c["starttime1"];
                    ms.EndTime = c["endtime1"];
                    ms.DescriptionText = c["description"];
                    ms.CommentText = c["comment"];
                    ms.UserId = userName;
                    ms.MeetingType = c["mtype"];
                    ms.Reminder = c["reminder"];
                    ms.MeetingId = Convert.ToInt32(c["ID"]);

                    var meetingtitle = ms.MeetingTitle;
                    var meetinglocation = ms.MeetingLocation;
                    var guests = ms.Guests;
                    var meetingdate = ms.MeetingDate.Value.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
                    var starttime = ms.StartTime;
                    var endtime = ms.EndTime;
                    var description = ms.DescriptionText;
                    var comment = ms.CommentText;
                   
                    var userId = ms.UserId;
                    var meetingtype = ms.MeetingType;
                    var reminder = ms.Reminder;
                    var meetingid = ms.MeetingId;

                    ViewData["meetingtitle"] = meetingtitle;
                    ViewData["meetinglocation"] = meetinglocation;
                    ViewData["guests"] = guests;
                    ViewData["meetingdate"] = meetingdate;
                    ViewData["starttime"] = starttime;
                    ViewData["endtime"] = endtime;
                    ViewData["description"] = description;
                    ViewData["comment"] = comment;
                    ViewData["userId"] = userId;
                    ViewData["meetingtype"] = meetingtype;
                    ViewData["reminder"] = reminder;
                   

                    var idKey = (from i in db.MeetingSchedulers
                                 orderby i.MeetingId descending
                                 select i.MeetingId).First();

                    Notification notification = new Notification();

                    var getGid = (from g in db.Students
                                  where userName == g.Std_Email
                                  select g.GId).FirstOrDefault();

                    var getPid = (from p in db.Groups
                                  where getGid == p.PId
                                  select p.PId).FirstOrDefault();

                    var getAdvisorEmail = (from proj in db.Projects
                                           where getPid == pro.PId
                                           select proj.AdvisorEmail).FirstOrDefault();


                    notification.MeetingId = idKey;
                    notification.AdvisorEmail = getAdvisorEmail;
                    notification.NText = ms.MeetingTitle;
                    notification.CurrentDate = DateTime.Now;
                    notification.GId = getGid;
                    db.Notifications.Add(notification);
                    db.SaveChanges();

                    var advisorUserId = from mss in db.MeetingSchedulers
                                        where mss.MeetingId == meetingid
                                        select mss;

                    foreach (var val in advisorUserId)
                    {

                        val.MeetingTitle = meetingtitle;
                        val.MeetingLocation = meetinglocation;
                        val.Guests = guests;
                        val.MeetingDate = DateTime.ParseExact(meetingdate, "dd/MM/yyyy", CultureInfo.InvariantCulture);
                        val.StartTime = starttime;
                        val.EndTime = endtime;
                        val.DescriptionText = description;
                        val.GId = getGid;
                        val.MeetingType = meetingtype;
                        val.Reminder = reminder;
                        val.CommentText = comment;
                        val.UserId = userId;
                    }

                    db.SaveChanges();

                    var Gidd = getGid;
                    ViewData["Gidd"] = Gidd;
                }

                var query = from g in db.Groups
                            select g;

                ViewData["grp"] = query.ToList();

                var projectTitle = from title in db.Projects
                                   select title.Title;

                ViewData["projecttitle"] = projectTitle.ToList();
                
            }
            catch (Exception e)
            {
                Console.WriteLine(e.InnerException);
            }
  
            return View("MeetingScheduler");
        }
    }
}